import fs from 'fs'
import path from 'path'

// 在Vite配置中添加一个插件，监听build事件
export default function buildLifeHook() {
  return {
    name: 'build-life-hook',
    buildStart() {
      const now = new Date().toLocaleString().replace(/\//g, '-')
      const version = { version: now }
      const versionPath = path.join(__dirname, '../../public/version/versionData.json')
      fs.writeFile(versionPath, JSON.stringify(version), 'utf8', (err) => {
        err ? console.log('写入文件失败') : console.log('写入文件成功')
      })
      console.log('构建开始！' + now)
    },
    buildEnd() {
      const now = new Date().toLocaleString().replace(/\//g, '-')
      console.log('构建完成！' + now)
    }
  }
}
